﻿using apitest.Application.Entities.Pms;
using Newtonsoft.Json.Linq;
using System.DirectoryServices.ActiveDirectory;
using System.Linq.Expressions;

namespace apitest.Application.Test.Services
{
    public class YearReportService 
    {
        ISqlSugarClient _db;
        public YearReportService(ISqlSugarClient db)
        {
            _db = db;
        }

        public async Task<dynamic> T1(YearReportSearcDto data)
        {

            var query = await GetQuery(data);
            var groups = new List<string>() { "HPLC","GC","NMR","MS","理化-其他"};
            var categoryList = await _db.Queryable<TestCategory>()
                .Where(o=>groups.Contains(o.Group1))
                .ToListAsync();
            var dataList= await query
                .Where((t1, t2) => t1.Status == TestRequestRecordStatusEnums.完成)
                .GroupBy((t1, t2) => new {t1.TestCategory,t1.StartDate.Value.Month})
                .Select((t1, t2) => new {
                    data1 =SqlFunc.AggregateSum(SqlFunc.IF(t1.DuringDate<=60).Return(1).End(0)),
                    data2 =SqlFunc.AggregateSum(SqlFunc.IF(t1.DuringDate <= 120).Return(1).End(0)),
                    data3 =SqlFunc.AggregateSum(SqlFunc.IF(t1.DuringDate <= 180).Return(1).End(0)),
                    data4 =SqlFunc.AggregateSum(SqlFunc.IF(t1.DuringDate > 180).Return(1).End(0)),
                    t1.TestCategory,
                    t1.StartDate.Value.Month
                })
                .ToListAsync();
            var res = new Dictionary<string, object>();
            var list = new List<dynamic>();
            for (int i = 1; i < 13; i++)
            {
                var dic = new Dictionary<string, int>();
                dic.Add("month", i);
                foreach (var group in groups)
                {
                    var groupList = categoryList
                        .Where(o => o.Group1 == group)
                        .Select(o=>o.Name)
                        .ToList();
                    var tempList = dataList
                        .Where(o => groupList.Contains(o.TestCategory) && o.Month == i)
                        .ToList();
                    dic.Add($"{group}_1", tempList.Sum(o => o.data1));
                    dic.Add($"{group}_2", tempList.Sum(o => o.data2));
                    dic.Add($"{group}_3", tempList.Sum(o => o.data3));
                    dic.Add($"{group}_4", tempList.Sum(o => o.data4));
                }
                list.Add(dic);
            }
            res.Add("list",list);
            return res;
        }

        private async Task<ISugarQueryable<TestRequestRecord, TestRequest>> GetQuery(YearReportSearcDto data, int dateType = 0)
        {
            if (string.IsNullOrWhiteSpace(data.Year))
            {
                throw new Exception("时间不能为空");
            }
            var start = DateTime.Parse($"{data.Year}-01-01");
            var end = start.AddMonths(1).AddSeconds(-1);
            var query = _db.Queryable<TestRequestRecord>()
                    .LeftJoin<TestRequest>((t1, t2) => t1.TestRequestId == t2.Id)
                    .WhereIF(dateType == 0, (t1, t2) => SqlFunc.Between(t1.StartDate, start, end))
                    .WhereIF(dateType == 1, (t1, t2) => SqlFunc.Between(t1.ResultDate, start, end))
                    .WhereIF(data.TestType.Count > 0, (t1, t2) => data.TestType.Contains(t2.TestType));

            if (data.LabId.HasValue && data.LabId != 0)
            {
                if (data.LabId == 1)
                {
                    query = query
                    .Where((t1, t2) => t2.LabId < 40);
                }
                else
                {
                    query = query
                     .Where((t1, t2) => t2.LabId == data.LabId);
                }
            }
            if (!string.IsNullOrWhiteSpace(data.Group2))
            {
                var categories = await _db.Queryable<TestCategory>()
                    .Where(o => o.Group2 == data.Group2)
                    .Select(o => o.Name)
                    .ToListAsync();
                query = query
                    .Where((t1, t2) => categories.Contains(t1.TestCategory));
            }
            return query;
        }

    }
    public class YearReportSearcDto
    {
        public string Year { get; set; }
        public int? LabId { get; set; }
        public string Group2 { get; set; }
        public List<string> TestType { get; set; }
    }
}
